package mdb

import (
	"fmt"
	"strings"
)

var Update update

type update struct {
	updateSql strings.Builder
	params    []interface{}
	tableName string
}

func NewUpdate(tableName string) *update {
	var params []interface{}
	return &update{tableName: tableName, updateSql: strings.Builder{}, params: params}
}
func (domain *update) UpdateParam() []interface{} {
	return domain.params
}
func (domain *update) UpdateSql() string {
	return domain.updateSql.String()
}
func (domain *update) Set(data map[string]interface{}) *update {
	update := &domain.updateSql
	update.WriteString(fmt.Sprintf("update %s set ", domain.tableName))
	len := len(data) - 1
	i := 0
	for k, v := range data {
		update.WriteString(fmt.Sprintf("%s=?", k))
		if i < len {
			update.WriteString(" , ")
		}
		i++
		domain.params = append(domain.params, v)
	}
	return domain
}
